package com.zenith.api.feign.user;

import com.zenith.common.result.PageResult;
import com.zenith.common.result.Result;
import com.zenith.api.po.dto.user.*;
import com.zenith.api.po.vo.user.PermissionVO;
import com.zenith.api.po.vo.user.RoleVO;
import com.zenith.api.po.vo.user.UserVO;
import com.zenith.api.po.vo.user.RolePermissionVO;
import com.zenith.api.po.vo.user.UserRoleVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 用户服务Feign客户端 - API服务
 */
@FeignClient(name = "zenith-middleware-service", path = "/middleware/user", contextId = "api-user-client")
public interface UserServiceFeignClient {
    
    /**
     * 检查用户是否是活跃状态
     */
    @Operation(summary = "检查用户是否是活跃状态")
    @PostMapping("/getUserActive")
    Result<Boolean> getUserActive(@RequestParam("userId") Long userId);

    /**
     * 获取当前用户信息接口
     */
    @Operation(summary = "获取当前用户信息")
    @PostMapping("/getCurrentUser")
    Result<Object> getCurrentUser(@RequestParam("userId") Long userId);

    // =========================== 用户管理相关接口 ===========================
    
    /**
     * 创建用户
     */
    @Operation(summary = "创建用户", description = "创建新的用户账户")
    @PostMapping("/createUser")
    Result<String> createUser(@Valid @RequestBody UserDTO userDTO);
    
    /**
     * 更新用户
     */
    @Operation(summary = "更新用户", description = "更新用户信息")
    @PostMapping("/updateUser")
    Result<String> updateUser(@Valid @RequestBody UserDTO userDTO);
    
    /**
     * 删除用户
     */
    @Operation(summary = "删除用户", description = "逻辑删除指定的用户")
    @PostMapping("/deleteUser/{id}")
    Result<String> deleteUser(@Parameter(description = "用户ID", required = true) @PathVariable @NotNull Long id);
    
    /**
     * 获取用户详情
     */
    @Operation(summary = "获取用户详情", description = "根据ID查询用户详细信息")
    @PostMapping("/getUserById/{id}")
    Result<UserVO> getUserById(
            @Parameter(description = "用户ID", required = true)
            @PathVariable @NotNull Long id);
    
    /**
     * 按用户名查询
     */
    @Operation(summary = "按用户名查询", description = "根据用户名查询用户信息")
    @PostMapping("/getUserByUsername")
    Result<UserVO> getUserByUsername(
            @Parameter(description = "用户名", required = true)
            @RequestParam @NotBlank String username);
    
    /**
     * 分页查询用户
     */
    @Operation(summary = "分页查询用户", description = "分页查询用户列表信息")
    @PostMapping("/getUserPage")
    Result<PageResult<UserVO>> getUserPage(@Valid @RequestBody UserDTO query);
    
    /**
     * 批量查询用户
     */
    @Operation(summary = "批量查询用户", description = "根据用户ID列表批量查询用户信息")
    @PostMapping("/getUsersByIds")
    Result<List<UserVO>> getUsersByIds(
            @Parameter(description = "用户ID列表", required = true)
            @RequestBody @NotEmpty List<Long> ids);
    
    /**
     * 更新用户状态
     */
    @Operation(summary = "更新用户状态", description = "启用或禁用用户账户")
    @PostMapping("/updateUserStatus/{id}")
    Result<String> updateUserStatus(
            @Parameter(description = "用户ID", required = true) @PathVariable @NotNull Long id,
            @Parameter(description = "状态（1-正常 2-禁用）", required = true) @RequestParam @NotNull Integer status);
    
    /**
     * 重置密码
     */
    @Operation(summary = "重置密码", description = "重置指定用户的登录密码")
    @PostMapping("/resetPassword/{id}")
    Result<String> resetPassword(
            @Parameter(description = "用户ID", required = true) @PathVariable @NotNull Long id,
            @Parameter(description = "新密码", required = true) @RequestParam @NotBlank String newPassword);
    
    /**
     * 获取当前用户信息（新接口）
     */
    @Operation(summary = "获取当前用户信息", description = "获取当前登录用户的详细信息")
    @PostMapping("/getCurrentUserInfo")
    Result<UserVO> getCurrentUserInfo(
            @Parameter(description = "用户ID", required = true)
            @RequestParam @NotNull Long userId);
    
    /**
     * 更新个人资料
     */
    @Operation(summary = "更新个人资料", description = "更新当前用户的个人资料")
    @PostMapping("/updateProfile")
    Result<String> updateProfile(@Valid @RequestBody UserDTO userDTO);
    
    /**
     * 修改密码
     */
    @Operation(summary = "修改密码", description = "修改用户登录密码")
    @PostMapping("/changePassword")
    Result<String> changePassword(
            @Parameter(description = "用户ID", required = true) @RequestParam @NotNull Long userId,
            @Parameter(description = "原密码", required = true) @RequestParam @NotBlank String oldPassword,
            @Parameter(description = "新密码", required = true) @RequestParam @NotBlank String newPassword);
    
    /**
     * 上传头像
     */
    @Operation(summary = "上传头像", description = "上传用户头像")
    @PostMapping("/uploadAvatar")
    Result<String> uploadAvatar(
            @Parameter(description = "用户ID", required = true) @RequestParam @NotNull Long userId,
            @Parameter(description = "头像URL", required = true) @RequestParam @NotBlank String avatarUrl);
    
    /**
     * 批量分配用户角色
     */
    @Operation(summary = "批量分配用户角色", description = "为多个用户批量分配角色")
    @PostMapping("/batchAssignRoles")
    Result<String> batchAssignRoles(
            @Parameter(description = "用户ID列表", required = true) @RequestParam @NotEmpty List<Long> userIds,
            @Parameter(description = "角色ID列表", required = true) @RequestParam @NotEmpty List<String> roleIds);
    
    /**
     * 批量删除用户（旧接口）
     */
    @Operation(summary = "批量删除用户（旧接口）")
    @PostMapping("/deleteUsers")
    Result<Void> deleteUsers(@RequestBody List<String> ids);
    
    /**
     * 高级搜索用户
     */
    @Operation(summary = "高级搜索用户", description = "按多条件搜索用户")
    @PostMapping("/searchUsers")
    Result<PageResult<UserVO>> searchUsers(
            @Parameter(description = "搜索关键词")
            @RequestParam(required = false) String keyword,
            @Parameter(description = "状态")
            @RequestParam(required = false) Integer status,
            @Parameter(description = "角色")
            @RequestParam(required = false) String role,
            @Parameter(description = "页码")
            @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量")
            @RequestParam(defaultValue = "10") Integer pageSize);

    // =========================== 权限管理相关接口 ===========================
    
    @Operation(summary = "根据ID查询权限")
    @PostMapping("/getPermissionById")
    Result<PermissionVO> getPermissionById(@RequestParam String id);

    @Operation(summary = "获取权限列表")
    @PostMapping("/getPermissionList")
    Result<List<PermissionVO>> getPermissionList();

    @Operation(summary = "分页查询权限")
    @PostMapping("/getPermissionPage")
    Result<PageResult<PermissionVO>> getPermissionPage(PermissionDTO query);

    @Operation(summary = "判断用户的角色是否拥有此权限")
    @GetMapping("/check")
    Result<Boolean> getPermissionIds(@RequestParam Long userId, @RequestParam String requiredPermission);

    @Operation(summary = "根据角色获取权限列表")
    @PostMapping("/selectPermissionsByRole")
    Result<List<PermissionVO>> selectPermissionsByRole(@RequestParam("role") String role);

    // =========================== 角色管理相关接口 ===========================
    
    @Operation(summary = "根据ID查询角色")
    @PostMapping("/getRoleById")
    Result<RoleVO> getRoleById(@RequestParam String id);

    @Operation(summary = "获取角色列表")
    @PostMapping("/getRoleList")
    Result<List<RoleVO>> getRoleList();

    @Operation(summary = "分页查询角色")
    @PostMapping("/getRolePage")
    Result<PageResult<RoleVO>> getRolePage(RoleDTO query);

    @Operation(summary = "更新角色")
    @PostMapping("/updateRole")
    Result<Void> updateRole(@RequestBody RoleVO entity);

    // =========================== 角色权限关联管理相关接口 ===========================
    
    @Operation(summary = "根据ID查询角色权限关联")
    @PostMapping("/getRolePermissionById")
    Result<RolePermissionVO> getRolePermissionById(@RequestParam String id);

    @Operation(summary = "获取角色权限关联列表")
    @PostMapping("/getRolePermissionList")
    Result<List<RolePermissionVO>> getRolePermissionList();

    @Operation(summary = "分页查询角色权限关联")
    @PostMapping("/getRolePermissionPage")
    Result<PageResult<RolePermissionVO>> getRolePermissionPage(RolePermissionDTO query);

    // =========================== 用户角色关联管理相关接口 ===========================
    
    @Operation(summary = "根据ID查询用户角色关联")
    @PostMapping("/getUserRoleById")
    Result<UserRoleVO> getUserRoleById(@RequestParam String id);

    @Operation(summary = "获取用户角色关联列表")
    @PostMapping("/getUserRoleList")
    Result<List<UserRoleVO>> getUserRoleList();

    @Operation(summary = "分页查询用户角色关联")
    @PostMapping("/getUserRolePage")
    Result<PageResult<UserRoleVO>> getUserRolePage(@RequestBody UserRoleDTO query);

    @Operation(summary = "获取用户角色ID列表")
    @PostMapping("/getUserRoleIds")
    Result<Set<String>> getUserRoleIds(@RequestParam("userId") Long userId);

    @Operation(summary = "获取用户权限ID列表")
    @PostMapping("/getUserPermissionIds")
    Result<Set<String>> getUserPermissionIds(@RequestParam("userId") Long userId);
}